package handler;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class AcceptHandler {

    private SelectionKey selectionKey;
    private Selector selector;

    public AcceptHandler() {
    }

    public AcceptHandler(SelectionKey selectionKey, Selector selector) {
        this.selectionKey = selectionKey;
        this.selector = selector;
    }

    public SelectionKey getSelectionKey() {
        return selectionKey;
    }

    public void setSelectionKey(SelectionKey selectionKey) {
        this.selectionKey = selectionKey;
    }

    public Selector getSelector() {
        return selector;
    }

    public void setSelector(Selector selector) {
        this.selector = selector;
    }

    public void handle(){
        try {
            ServerSocketChannel serversocketChannel = (ServerSocketChannel)selectionKey.channel();
            SocketChannel socketChannel = serversocketChannel.accept();
            socketChannel.configureBlocking(false);
            socketChannel.register(selector,SelectionKey.OP_READ);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
